package com.example.demo.service;

import com.example.demo.common.domain.*;
import com.example.demo.mapper.*;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {
    @Autowired
    private OrderListMapper orderListMapper;
    @Autowired
    private HorderMapper horderMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private ReviewExample reviewExample;

    public Horder searchHorder(String orderNumber) {
        return horderMapper.selectByPrimaryKey(orderNumber);
    }

    public Hotel searchHotel(String hotelId) {
        return hotelMapper.selectByPrimaryKey(hotelId);
    }

    public Room searchRoom(Integer rtypeId) {
        return roomMapper.selectByPrimaryKey(rtypeId);
    }

    public List<Review> searchReview(String HotelName) {
        reviewExample.or().andHNameEqualTo(HotelName);
        return reviewMapper.selectByExample(reviewExample);
    }

    public Map selectHAndRName(Room room, String[] InDate, String[] OutDate) {
        String roomTypeName = roomMapper.selectByPrimaryKey(room.getRtypeId()).getRtypeName();
        String hotelName = hotelMapper.selectByPrimaryKey(room.getHotelId().toString()).getHotelTranslatedName();
        Integer roomPrice = roomMapper.selectByPrimaryKey(room.getRtypeId()).getPrice();
        Integer[] CheckInData = (Integer[]) ConvertUtils.convert(InDate, Integer.class);
        Integer[] CheckOutData = (Integer[]) ConvertUtils.convert(OutDate, Integer.class);
        LocalDate FirstDate = LocalDate.of(CheckInData[0], CheckInData[1], CheckInData[2]);
        LocalDate SecordDate = LocalDate.of(CheckOutData[0], CheckOutData[1], CheckOutData[2]);
        Period p = Period.between(FirstDate, SecordDate);
        Map map = new HashMap();
        map.put("dataDiffient", p.getDays());
        map.put("roomTypeName", roomTypeName);
        map.put("hotelName", hotelName);
        map.put("roomPrice", roomPrice);
        return map;
    }

    public Horder makeOrder(Horder horder, String uName) {
        Room room = roomMapper.selectByPrimaryKey(horder.getRtypeId());
        Hotel hotel = hotelMapper.selectByPrimaryKey(room.getHotelId().toString());
        String OrderNumber = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS"));
        String OrderTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
        Horder horder1 = new Horder();
        horder1.setOrderNumber(OrderNumber);
        horder1.setHotelId(room.getHotelId());
        horder1.setuName(uName);
        horder1.setRtypeId(room.getRtypeId());
        horder1.setHotelName(hotel.getHotelTranslatedName());
        horder1.setOrderTime(OrderTime);
        horder1.setInTime(horder.getInTime());
        horder1.setOutTime(horder.getOutTime());
        horder1.setRoomCount(horder.getRoomCount());
        horder1.setRname(horder.getRname());
        horder1.setPhone(horder.getPhone());
        horder1.setPrice(horder.getPrice());
        horder1.setOrderState("已支付");
        return horder1;
    }
}
